package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.entity.NoticeEntity;
import com.kla.power.mapper.NoticeMapper;
import com.kla.power.service.INoticeService;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工单通知 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-06-24
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeEntity> implements INoticeService {

    @Override
    public List<NoticeEntity> queryList (NoticeEntity noticeEntity) {
        if ( noticeEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<NoticeEntity> noticeEntities = baseMapper.selectList ( new QueryWrapper<NoticeEntity> ().eq ( "userId", noticeEntity.getUserId () ) );
        return noticeEntities;
    }

    @Override
    public NoticeEntity querySingle (NoticeEntity noticeEntity) {
        if ( noticeEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( noticeEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        NoticeEntity notice = baseMapper.selectById ( noticeEntity );
        // 修改已读
        if ( notice != null ) {
            notice.setIsRead ( 1 );
            int update = baseMapper.update ( notice, new UpdateWrapper<> () );
            if ( update == 0 ) {
                throw new StateException ( StateEnum.UPDATE_EXCE );
            }
        }
        return notice;
    }

}
